import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',//登陆
    name: 'login',
    component: ()=>import('../views/keji/LoginView.vue')
  },
  {
    path: '/register',//注册
    name: 'register',
    component: () => import('../views/keji/RegisterView.vue')
  },
  {
    path: '/home',//主页
    name: 'home',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/HomePage.vue')
  },
  {
    path: '/question',//问答展示
    name: 'question',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/QuestionView.vue')
  },
  {
    path: '/blog/:id',//博客展示
    name: 'blog',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/BlogView.vue')
  },
  {
    path: '/information',//个人信息
    name: 'information',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/InformationView.vue')
  },
  {
    path: '/account',//班级账单
    name: 'account',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/TableView.vue')
  },
  {
    path: '/table',//编辑班级账单
    name: 'table',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/TableView.vue')
  },
  {
    path: '/editcompus',//编辑校园博客界面
    name: 'editcompus',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/EditCompusView.vue')
  },
  {
    path: '/editclass',//编辑班级博客界面
    name: 'editclass',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/EditClassView.vue')
  },
  {
    path: '/notice',//班级通知界面
    name: 'notice',
    meta: { requiresAuth: true },
    component: ()=>import('../views/keji/NoticeView.vue')
  },
  { //设置默认跳转
    path: '/',
    redirect: '/login'
  }
]

const router = new VueRouter({
  routes
})
 
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.matched.some(record => record.meta.requiresAuth) && !token) next({ name: 'login' })
  else next()
})

export default router
